package com.michale.admin.controller.tool;

import com.github.pagehelper.PageInfo;
import com.michale.common.annotation.Log;
import com.michale.common.domain.tool.JobEntity;
import com.michale.common.enmus.BusinessType;
import com.michale.common.result.AjaxResult;
import com.michale.common.result.PageResult;
import com.michale.system.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author michale
 * @apiNote 定时任务调度 控制层
 */
@RestController
@RequestMapping("/tool/job")
public class JobController extends BaseController {
    @Autowired
    private JobService jobService;

    /**
     * 查询定时任务调度列表
     */
    @GetMapping("/jobList")
    @PreAuthorize("@security.isPermission('tool:job:list')")
    public PageResult JobList(JobEntity jobEntity) {
        startPage();
        List<JobEntity> jobList = jobService.selectJobList(jobEntity);
        PageInfo<JobEntity> pageInfo = new PageInfo<>(jobList);
        return pageResult(pageInfo);
    }

    /**
     * 获取定时任务调度详细信息
     */
    @GetMapping(value = "/{jobId}")
    @PreAuthorize("@security.isPermission('tool:job:query')")
    public AjaxResult getInfo(@PathVariable("jobId") Long jobId) {
        return success(jobService.selectJobByJobId(jobId));
    }

    /**
     * 新增定时任务调度
     */
    @PostMapping("insertJob")
    @Log(title = "定时任务调度", businessType = BusinessType.INSERT)
    @PreAuthorize("@security.isPermission('tool:job:add')")
    public AjaxResult insertJob(@RequestBody JobEntity jobEntity) {
        return toAjax(jobService.insertJob(jobEntity));
    }

    /**
     * 修改定时任务调度
     */
    @PutMapping("updateJob")
    @Log(title = "定时任务调度", businessType = BusinessType.UPDATE)
    @PreAuthorize("@security.isPermission('tool:job:edit')")
    public AjaxResult edit(@RequestBody JobEntity jobEntity) {
        return toAjax(jobService.updateJob(jobEntity));
    }
    /**
     * 修改定时任务调度状态
     */
    @PutMapping("/changeStatus")
    @Log(title = "定时任务调度", businessType = BusinessType.UPDATE)
    @PreAuthorize("@security.isPermission('tool:job:edit')")
    public AjaxResult changeStatus(@RequestBody  JobEntity jobEntity) {
        return toAjax(jobService.changeStatus(jobEntity));
    }

    /**
     * 删除定时任务调度
     */
    @DeleteMapping("/deleteJob/{jobIds}")
    @Log(title = "定时任务调度", businessType = BusinessType.DELETE)
    @PreAuthorize("@security.isPermission('tool:job:remove')")
    public AjaxResult remove(@PathVariable Long[] jobIds) {
        return toAjax(jobService.deleteJobByJobIds(jobIds));
    }
}
